/**
 * DateUtil.java
 * 
 * Create Version: 1.0
 * Author: lixh
 * Create Date: 2014年11月6日
 * 
 * Copyright (c) 2014 KOP. All Right Reserved.
 */
package com.yinhai.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * DateUtil （时间格式化）
 *
 * @author lixh
 */
public class DateUtil {
	public static final String DAY_FORMATE_CN = "yyyy年MM月dd日";
	public static final String DAY_FORMATE = "yyyy-MM-dd";
	public static final String DAY_TIME_FORMATE = "yyyy-MM-dd HH:mm:ss";

	/**
	 * 字符串格式的日期转换为Date
	 * 
	 * @param dateStr
	 * @param formate
	 * @return
	 */
	public static Date stringFormateDate(String dateStr, String formate) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(formate);
		Date date = new Date();
		if (NullUtils.isEmpty(dateStr)) {
			return date;
		}
		try {
			date = dateFormat.parse(dateStr);
		} catch (ParseException e) {
		}
		return date;
	}

	public static String dateFormatString(Date date, String formate) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(formate);
		if (NullUtils.isNull(date)) {
			date = new Date();
		}
		return dateFormat.format(date);
	}

	public static String dateTimeFormatString(String longTime) {
		Long time = Long.valueOf(longTime);
		Date date = new Date(time);
		return dateFormatString(date, DAY_TIME_FORMATE);
	}

	public static String dateLongFormatString(Long longTime, String formate) {
		Long time = Long.valueOf(longTime);
		Date date = new Date(time);
		return dateFormatString(date, formate);
	}

	/**
	 * 判断星期几 星期天等于7
	 * 
	 * @param date
	 * @return
	 */
	public static int getWeek(Date date) {

		Calendar c = Calendar.getInstance();
		c.setTime(date);

		int week = c.get(Calendar.DAY_OF_WEEK);
		if (week == 1) {
			week = 7;
		} else {
			week = week - 1;
		}
		// 7-(8-week)%7;
		return week;
	}

	/**
	 * 获取月初
	 * @param date
	 * @return
	 */
	public static String getMonthStart(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int month = (c.get(Calendar.MONTH)) + 1;
		return c.get(Calendar.DAY_OF_YEAR) + "-"
				+ (month > 9 ? (month + "") : "0" + month) + "-" + "01";
	}
	/**
	 * 
	 *判断当前时间是否操作指定时间范围，type为1表示天，2表示周，3表示月
	 */
	public static boolean isAfterDays(Date currentDate,Date beginDate,String type) {
		boolean flag = false;
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //制定日期格式
		Calendar c=Calendar.getInstance();
		c.setTime(beginDate);
		if("1".equals(type)) {
			c.add(Calendar.DATE,1);
		} else if("2".equals(type)) {
			c.add(Calendar.WEDNESDAY,1);
		} else if("3".equals(type)) {
			c.add(Calendar.MONTH,1); 
		}
		if(currentDate.getTime() > c.getTime().getTime()) {
			flag = true;
		}
		return flag;
	}
}
